#!/bin/bash

_ArrayNum=0
_BranchPath="null"
_LogDir=/home/libin/zwx1089978/files/shell

# Creating a Dynamic Array
while [[ -n ${_BranchPath} ]]
do
    printf "\033[33m[InPut] \033[0m"
    read -e -p "sync branch path: " _BranchPath
    _ArrayPathName[${_ArrayNum}]=${_BranchPath}
    let _ArrayNum++
done

# Traversing Arrays
src_path=`pwd`
let _ArrayNum-=2
while [[ $_ArrayNum -ge 0 ]]
do
    printf "\033[32m[****** sync branch ---> ${_ArrayPathName[$_ArrayNum]} ******]\033[0m \n"
    cd ${_ArrayPathName[$_ArrayNum]}
    if [ $? -ne 0 ];then
        printf  "\033[31m[****** Error Path: ${_ArrayPathName[$_ArrayNum]} ******]\033[0m \n"
        exit 1
    else
        # log=$_LogDir/`date "+%Y%m%d%H%M%S"`.log
        repo sync -c --no-tags -j4
        if [ $? -ne 0 ];then
            printf  "\033[31m[****** Error sync: ${_ArrayPathName[$_ArrayNum]} *****]\033[0m \n"
            exit 1
        fi
        cd $src_path
    fi
    let _ArrayNum--
done
